package com.whacos.modules.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whacos.common.entity.ScheduleJob;
import com.whacos.common.enums.Constant;
import com.whacos.common.quartz.utils.QuartzManager;
import com.whacos.common.shiro.ShiroUtils;
import com.whacos.common.utils.DateUtils;
import com.whacos.common.utils.ObjectUtils;
import com.whacos.common.utils.ScheduleJobUtils;
import com.whacos.modules.system.dao.SysTaskDao;
import com.whacos.modules.system.entity.SysTask;
import com.whacos.modules.system.service.SysTaskService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * @author xiongdun
 * @description SysJobServiceImpl
 * @since 2018/11/17 17:11
 */
@Service
public class SysTaskServiceImpl extends ServiceImpl<SysTaskDao, SysTask> implements SysTaskService {

    @Autowired
    private SysTaskDao sysTaskDao;

    @Autowired
    private QuartzManager quartzService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeById(Serializable id) {
        try {
            SysTask scheduleJob = this.getById(id);
            this.quartzService.deleteJob(ScheduleJobUtils.entityToData(scheduleJob));
            return sysTaskDao.deleteById(id) > 0;
        } catch (SchedulerException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean batchRemove(List<Long> idList) {
        for (Long id : idList) {
            try {
                SysTask scheduleJob = this.getById(id);
                this.quartzService.deleteJob(ScheduleJobUtils.entityToData(scheduleJob));
            } catch (SchedulerException e) {
                e.printStackTrace();
                return false;
            }
        }
        return this.sysTaskDao.deleteBatchIds(idList) > 0;
    }

    @Override
    public void initSchedule() throws SchedulerException {
        // 这里获取任务信息数据
        List<SysTask> jobList = this.sysTaskDao.selectList(null);
        for (SysTask sysTask : jobList) {
            if ("1".equals(sysTask.getStatus())) {
                ScheduleJob job = ScheduleJobUtils.entityToData(sysTask);
                quartzService.addJob(job);
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeStatus(Long jobId, String cmd) throws SchedulerException {
        SysTask sysTask = this.getById(jobId);
        if (ObjectUtils.isEmpty(sysTask)) {
            return;
        }
        if (Constant.STATUS_RUNNING_STOP.equals(cmd)) {
            quartzService.deleteJob(ScheduleJobUtils.entityToData(sysTask));
            sysTask.setStatus(ScheduleJob.STATUS_NOT_RUNNING);
        } else {
            if (!Constant.STATUS_RUNNING_START.equals(cmd)) {
                // 空判断
            } else {
                sysTask.setStatus(ScheduleJob.STATUS_RUNNING);
                quartzService.addJob(ScheduleJobUtils.entityToData(sysTask));
            }
        }
        sysTask.updatedData();
        this.updateById(sysTask);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateCron(Long jobId, String cron) throws SchedulerException {
        SysTask sysTask = this.getById(jobId);
        if (ObjectUtils.isEmpty(sysTask)) {
            return;
        }
        if (ScheduleJob.STATUS_RUNNING.equals(sysTask.getStatus())) {
            quartzService.updateJobCron(ScheduleJobUtils.entityToData(sysTask));
        }
        sysTask.setCronExpression(cron);
        sysTask.updatedData();
        this.updateById(sysTask);
    }

}
